﻿using System.IO;
using System.Linq;
using System.Web.Helpers;
using System.Web.Mvc;
using SportClubMvc.Model;

namespace SportClubMvc.Controllers
{
    public class HomeController : Controller
    {
        private readonly SportsClubDbContext _dbContext = new SportsClubDbContext();

        public ActionResult Index()
        {
            var allNewsItems = _dbContext.News.ToList();

            foreach (var newsItem in allNewsItems)
            {
                if (newsItem.Ingress == null)
                {
                    newsItem.Ingress = GenerateIngress(newsItem.Text);
                }
            }
            var sortedList = allNewsItems.OrderByDescending(o => o.DateCreated).ToList();

            return View(sortedList);
        }

        private string GenerateIngress(string text)
        {
            return text.Length <= 299 ? text : text.Substring(0, 300);
        }

        public ActionResult Stadium()
        {
            return View("Holt");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public void GetCarouselPhoto(string filename, string imagetext, int width, int height)
        {
            var path = Path.Combine(Server.MapPath("~/Content/images/news"), filename);
            new WebImage(path).Resize(width, height, false)
                              .Crop(1, 1)
                              .AddTextWatermark(imagetext,fontColor:"White", fontSize:14, horizontalAlign: "Left")
                              .Write();
        }
    }
}
